# app/rag/embedding/factory.py

from .ollama import OllamaEmbedding
from .huggingface import HuggingFaceEmbedding
from .openai import OpenAIEmbedding
from .custom import CustomEmbedding
from ...core.config import settings
from ...core.logger import logger


def create_embedding_provider():
    """根据配置创建对应的 Embedding 提供者"""
    # 根据配置文件中的设置来决定使用的embedding提供者
    provider_name = settings.EMBEDDING_TYPE

    if provider_name == "ollama":
        return OllamaEmbedding(model=settings.OLLAMA_EMBEDDING_MODEL, base_url=settings.OLLAMA_BASE_URL)

    elif provider_name == "huggingface":
        # HuggingFace需要额外的配置，暂时使用默认设置
        return HuggingFaceEmbedding(model_name=settings.EMBEDDING_MODEL, device="cpu")

    elif provider_name == "openai":
        # OpenAI需要API密钥等配置
        if not settings.EMBEDDING_API_KEY:
            raise ValueError("OpenAI API密钥未配置，请设置环境变量或修改配置")
        return OpenAIEmbedding(
            model=settings.EMBEDDING_MODEL,
            api_key=settings.EMBEDDING_API_KEY,
            api_base=settings.EMBEDDING_API_BASE,
            timeout=30,
        )
    
    elif provider_name == "custom":
        # 自定义嵌入服务，用于支持自搭建的模型
        return CustomEmbedding(
            model=settings.EMBEDDING_MODEL,
            api_base=settings.EMBEDDING_API_BASE,
            api_key=settings.EMBEDDING_API_KEY
        )

    else:
        # 默认使用 Ollama
        return OllamaEmbedding(model=settings.OLLAMA_EMBEDDING_MODEL, base_url=settings.OLLAMA_BASE_URL)